package com.qcby.service.impl;

import com.qcby.dao.CheckingsDao;
import com.qcby.entity.*;
import com.qcby.service.CheckingsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckingsServiceImpl implements CheckingsService {

    @Autowired
    private CheckingsDao checkingsDao;

    @Override
    public Result<Integer> getUserIdByName(String userName) {
        return Result.success(checkingsDao.getUserIdByName(userName).getId());
    }

    @Override
    public Result<User> findUserInfoByUserId(int userId) {
        return Result.success(checkingsDao.findUserInfoByUserId(userId));
    }

    @Override
    public Result<List<UserCheckin>> getCheckinsByUserId(@Param("Uid") Integer Uid, @Param("shopId") Integer shopId) {
        System.out.println("checkingsDao.getCheckinsByUserId(Uid,shopId)");
        System.out.println(checkingsDao.getCheckinsByUserId(Uid,shopId));
        return Result.success(checkingsDao.getCheckinsByUserId(Uid,shopId));
    }

    @Override
    public Result<List<Review>> getCommentsByCheckinId(int checkinId) {
        List<Review> reviews = checkingsDao.getCommentsByCheckinId(checkinId);
        for (Review review :
                reviews) {
            int userId = review.getUserId();
            User userInfo = checkingsDao.getUserInfoByUserId(userId);
            review.setUser(userInfo);
//            System.out.println("userInfo = " + userInfo);
        }
        return Result.success(reviews);
    }

    @Override
    public Result<List<Review>> getChildrenCommentsByParentId(int parentId) {
        List<Review> reviews = checkingsDao.getChildrenCommentsByParentId(parentId);
        for (Review review :
                reviews) {
            int userId = review.getUserId();
            User userInfo = checkingsDao.getUserInfoByUserId(userId);
            review.setUser(userInfo);
//            System.out.println("userInfo = " + userInfo);
        }
        return Result.success(reviews);
    }

    @Override
    public Result<Integer> addComment(int checkinId, int userId, String content) {

        //查询当前打卡记录的评论数
        int singleReviewCount = checkingsDao.findCheckinReviewCount(checkinId);

        //在评论的同时更新评论数
        checkingsDao.updateCommentsCount(singleReviewCount, checkinId);

        checkingsDao.addComment(checkinId, userId, content);

        return Result.success(singleReviewCount+1);
    }

    @Override
    public Result<Integer> replyComment(int checkinId, int userId, String content, int parentId) {

        //查询当前打卡记录的评论数
        int singleReviewCount = checkingsDao.findCheckinReviewCount(checkinId);

        //在评论的同时更新评论数
        checkingsDao.updateCommentsCount(singleReviewCount, checkinId);

        checkingsDao.replyComment(checkinId, userId, content, parentId);

        return Result.success(singleReviewCount+1);
    }

    @Override
    public Result<Integer> addCheckin(Checkings checkings) {
        return Result.success(checkingsDao.addCheckin(checkings));
    }
    @Override
    public Result<Integer> likeCheckin(int checkinId, int userId) {

        checkingsDao.likeCheckin(checkinId, userId);
        int likeCount = checkingsDao.findLikeCount(checkinId);
        checkingsDao.LikeUpdateCheckin(likeCount, checkinId);
        likeCount = checkingsDao.findLikeCount(checkinId);
        return Result.success(likeCount);
    }

    @Override
    public Result<Integer> cancelCheckinLike(int checkinId, int userId) {
        int likeCount = checkingsDao.findLikeCount(checkinId);
        checkingsDao.cancelCheckinLike(checkinId, userId);
        checkingsDao.cancelLikeUpdateCheckin(likeCount, checkinId);
        return Result.success(checkingsDao.cancelCheckinLike(checkinId, userId));
    }


    @Override
    public Result<Integer> likeComment(int commentId, int userId) {
        checkingsDao.likeComment(commentId, userId);
        int likeCount = checkingsDao.findCommentLikeCount(commentId);
        checkingsDao.LikeUpdateComment(likeCount, commentId);
        likeCount = checkingsDao.findCommentLikeCount(commentId);
        return Result.success(likeCount);

    }

    @Override
    public Result<Integer> cancelCommentLike(int commentId, int userId) {
        int likeCount = checkingsDao.findCommentLikeCount(commentId);
        checkingsDao.cancelCommentLike(commentId, userId);
        checkingsDao.cancelLikeUpdateComment(likeCount, commentId);
        return Result.success(checkingsDao.cancelCommentLike(commentId, userId));
    }

    @Override
    public Result<List<Integer>> getUserLikedCheckins(int userId) {
        return Result.success(checkingsDao.getUserLikedCheckins(userId));
    }

    @Override
    public Result<List<Integer>> getUserLikedComments(int userId, int checkinId) {
        return Result.success(checkingsDao.getUserLikedComments(userId, checkinId));
    }

    @Override
    public Result<Integer> updateShareStatus(int checkinId, int isShared, int userId) {
        return Result.success(checkingsDao.updateShareStatus(checkinId, isShared, userId));
    }

}
